package com.example.administrator.szb.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

/**
 * Created by stone on 2016/8/24.
 */
public class AppUtils {
    private AppUtils()
    {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");

    }

    /**
     * 获取应用程序名称
     */
    public static String getAppName(Context context)
    {
        try
        {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            int labelRes = packageInfo.applicationInfo.labelRes;
            return context.getResources().getString(labelRes);
        } catch (PackageManager.NameNotFoundException e)
        {
            e.printStackTrace();
        }
        return null;
    }


    /***
     * 获取包名
     * @param context
     * @return
     */
    public static String getPacketName(Context context){
        try
        {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            return packageInfo.packageName;

        } catch (PackageManager.NameNotFoundException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * [获取应用程序版本名称信息]
     *
     * @param context
     * @return 当前应用的版本名称
     */
    public static String getVersionName(Context context)
    {
        try
        {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            return packageInfo.versionName;

        } catch (PackageManager.NameNotFoundException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 返回版本号
     * 对应build.gradle中的versionCode
     *
     * @param context
     * @return
     */
    public static int getVersionCode(Context context) {
        int code = 0;
        String versionCode = null;
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            versionCode = String.valueOf(packInfo.versionCode);
            code = Integer.parseInt(versionCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return code;
    }

    /**
     * 获取是从哪个市场下载的
     * @param context
     * @return
     */
    public static int getFromMarket(Context context){
        int marketCode = 0;
        try {
            ApplicationInfo appInfo = context.getPackageManager()
                    .getApplicationInfo(context.getPackageName(),
                            PackageManager.GET_META_DATA);
            marketCode = appInfo.metaData.getInt("DOWNLOAD_SOURCE", 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return marketCode;
    }

    public static void isClearData(){
        /*int code = getVersionCode(SampleApplicationLike.getInstance());//当前版本号
        int code2 = SPUtils.getDatasInt("v_code");
        if (code2==-1){
            SPUtils.clear();
        }
        SPUtils.setDatas("v_code",code);*/
    }
}
